package com.superquan.selfstudybootmybatis.utils.pdfanalysis;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author zwq
 * @description pdf相关方法
 * @date 2022/6/23
 */
@Slf4j
public class PdfUtil {
    /**
     *
     * @param o 写入的数据
     * @param out 自定义保存pdf的文件流
     * @param templatePath pdf模板路径
     */
    // 利用模板生成pdf
    public  void fillTemplate(Map<String,Object> o, ServletOutputStream out, String templatePath) {
        PdfReader reader;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            java.util.Iterator<String> it = form.getFields().keySet().iterator();
            while (it.hasNext()) {
                String name = it.next().toString();
                System.out.println(name);
                String value = o.get(name)!=null?o.get(name).toString():null;
                form.setField(name,value);
            }
            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.close();

            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();

        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }
    }

    // 利用模板生成pdf
    public static void fillPdfTemplate(Map<String,Object> map) {
        // 原始模板路径
        String templatePath = "E:/processPdf/invoice.pdf";
        // 生成的新pdf文件路径
        String newPDFPath = "E:/processPdf/newInvoice.pdf";
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfReader reader= null;
        PdfStamper stamper = null;
        try {
            // 输出流
            out = new FileOutputStream(newPDFPath);
            // 读取pdf模板
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
//            String[] str = {"Number":"202206241754","DataTime":"2022年6月24日","Unit":"北京赢销通有限公司","SystemName":"固特异轮胎有限公司","InvoiceValue":"2000.00.00"};
            //获取pdf设置的表单域-fields
            java.util.Iterator<String> it = form.getFields().keySet().iterator();
            while (it.hasNext()) {
                String name = it.next().toString();
                String value = String.valueOf(map.get(name));
                System.out.println(name);
                form.setField(name, value);
            }
            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
            if (stamper != null) {
                try {
                    stamper.close();
                } catch (DocumentException e) {
                    log.error(e.getMessage(), e);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            Document doc = new Document();
            //制作pdf文档副本
            PdfCopy copy = new PdfCopy(doc, out);
            //打开文档，并开始向文档正文添加正文
            doc.open();
            //将读取的pdf模板内容导入到另一个页面
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
        } finally {
//
            if (reader != null) {
                reader.close();
            }
        }

    }
}
